package org.nupter.mmchat.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.avos.avoscloud.LogInCallback;
import com.avos.avoscloud.ParseException;
import com.avos.avoscloud.ParseUser;
import com.avos.avoscloud.SignUpCallback;
import org.nupter.mmchat.R;

/**
 * Created with IntelliJ IDEA.
 * User: linxiangyu
 * Date: 13-10-8
 * Time: 上午11:57
 * To change this template use File | Settings | File Templates.
 */
public class RegisterActivity extends Activity {
    EditText editText;
    TextView textView;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        editText = (EditText) findViewById(R.id.editText);
        Button button = (Button)findViewById(R.id.button);
        textView =(TextView)findViewById(R.id.textView);


        String username;
        if (ParseUser.getCurrentUser().getUsername() != null)
            username = ParseUser.getCurrentUser().getUsername();
        else
            username = "No User";

        textView.setText(username);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = editText.getText().toString();
                name = "oa414";
                ParseUser user = new ParseUser();
                user.setUsername(name);
                user.setEmail("oa414@163.COM");
                user.setPassword("123456");
                Toast.makeText(RegisterActivity.this, "ZHUCEZHONG", Toast.LENGTH_SHORT).show();

                user.signUpInBackground(new SignUpCallback() {
                    public void done(ParseException e) {
                        if (e == null) {
                            Toast.makeText(RegisterActivity.this, "succuss", Toast.LENGTH_SHORT).show();

                            ParseUser.logInInBackground("oa414", "123456", new LogInCallback() {
                                public void done(ParseUser user, ParseException e) {
                                    if (user != null) {
                                        Toast.makeText(RegisterActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(RegisterActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        } else {
                            Toast.makeText(RegisterActivity.this, e.toString(), Toast.LENGTH_SHORT).show();


                        }
                    }
                });




            }
        });
    }

}